// 云函数入口文件
const cloud = require('wx-server-sdk')
const TcbRouter = require('tcb-router');
const returnUtil = require('utils/ReturnUtil.js')
const login = require('service/userService.js')//商品分类
const banner = require('service/bannerService.js')//banner
const theme = require('service/themeService.js')//主题
const product = require('service/productService.js')//商品分类
const shopcart = require('service/shopcartService.js')//购物车
const order = require('service/orderService.js')//订单类
const baseTest = require('test/models/BaseModelTest.js')
cloud.init()
const IMAGEPREFIX = "cloud://yun-duan-8693e.7975-yun-duan-8693e-1302866574/"


// 云函数入口函数
exports.main = async (event, context) => {
    const app = new TcbRouter({ event });
    // app.use 表示该中间件会适用于所有的路由
    app.use(async (ctx, next) => {
      ctx.data = {};
      await next(); // 执行下一中间件
    });
    /***************************    用户登录   *****************************************/
    // 获取用户信息
    app.router('addUser', async (ctx, next) => {
      ctx.data = await login.addUserService(event.data)
      console.log(ctx.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 用户验证登录
    app.router('getUser', async (ctx, next) => {
      ctx.data = await login.getUserService(event.data)
      console.log(ctx.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })


    /***************************    首页   *****************************************/
    // 获取轮播
    app.router('getBanner', async (ctx, next) => {
      ctx.data = await _bannerItem(banner.getBanner())
      ctx.body = await returnUtil.success(ctx)
      await next();
    })
    // 获取主题
    app.router('getTheme', async (ctx, next) => {
      ctx.data = await _themeItem(theme.getTheme())
      ctx.body = await returnUtil.success(ctx)
      await next()
    })

    // /***************************    分类   *****************************************/
    // 获取分类
    app.router('getCategoryMenu', async (ctx, next) => {
      ctx.data = await product.getCategoryMenu()
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 获取分类商品
    app.router('getCategoryProduct', async (ctx, next) => {
      // ctx.data 前台传过来的id
      ctx.data = await _productItem(product.getCategoryProduct(event.data))
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 搜索分类商品
    app.router('SearchProduct', async (ctx, next) => {
      console.log(event)
      // ctx.data 前台传过来的id
      ctx.data = await _productItem(product.SearchProduct(event.data))
      console.log(ctx.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 获取商品详情
    app.router('getDetailProduct', async (ctx, next) => {
      console.log(event)
      ctx.data = await _productImg(product.getDetailProduct(event.data))
      console.log(ctx.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
  
    // /***************************    购物车信息   *****************************************/

    // 添加购物车
    app.router('addCart', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.addCartService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 查询购物车
    app.router('getShopcart', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.getCartService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 删除购物车
    app.router('removeShopcart', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.removeCartService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 更新购物车
    app.router('updatedShopcart', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.updatedCartService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 提交订单页
    app.router('getShopcartById', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.getByIdService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 添加收货地址
    app.router('addAddress', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.addAddressService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 查询收货地址
    app.router('getAdress', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.getAdressService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 更新收货地址
    app.router('updatedAddress', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.updatedAddressService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // 删除收货地址
    app.router('removeAddress', async (ctx, next) => {
      console.log(event)
      ctx.data = await shopcart.removeAddressService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // /***************************    订单   *****************************************/
    // 生成订单
    app.router('addOrder', async (ctx, next) => {
      console.log(event)
      ctx.data = await order.addOrderService(event.data)
      ctx.body = await returnUtil.success(ctx)
      await next()
    })
    // /***************************    商品信息   *****************************************/
  
    // // 获取商品信息
    // app.router('getProductById', async (ctx, next) => {
    //   let product_id = event.data.product_id
    //   ctx.data = await _productImg(product.getProductById(product_id))
    //   ctx.body = await returnUtil.success(ctx)
    //   await next()
    // })
  
  
    // /***************************    主题商品   *****************************************/
    // // 获取主题商品列表
    // app.router('getThemeProduct', async (ctx, next) => {
    //   let theme_type = event.data.theme_type
    //   ctx.data = await _productItem(product.getThemeProduct(theme_type))
    //   ctx.body = await returnUtil.success(ctx)
    //   await next()
    // })
  
    // /***************************    订单   *****************************************/
    // // 生成订单
    // app.router('creatOrder', async (ctx, next) => {
    //   //event.data.orderData,event.userInfo
    //   ctx.data = await order.create(event.data.orderData, event.userInfo)
    //   ctx.body = await returnUtil.success(ctx)
    //   await next()
    // })
  
    // // 根据订单获取信息
    // app.router('getOrderById', async (ctx, next) => {
    //   let orderId = event.data.orderId
    //   ctx.data = await order.getOrderById(orderId)
    //   ctx.body = await returnUtil.success(ctx)
    //   await next()
    // })
  
    // // 获取订单信息
    // app.router('getOrderList', async (ctx, next) => {
    //   ctx.data = await order.getOrderList(event.userInfo)
    //   ctx.body = await returnUtil.success(ctx)
    //   await next()
    // })
  
  
    // /***************************    测试   *****************************************/
  
    // app.router('tests', async (ctx, next) => {
    //   // test 可参数类型 是否决定传参
    //   ctx.data = await baseTest.test()
    //   ctx.body = await returnUtil.success(ctx);
    //   await next();
    // })
  
    // /***************************    云函数调用   *************************************/
  
    // app.router('callFunc', async (ctx, next) => {
    //   // test 可参数类型 是否决定传参
    //   ctx.data = "云函数之间的调用"
    //   ctx.body = await returnUtil.success(ctx);
    //   await next();
    // })
  
  
  
  
  
  
    // 轮播图片地址拼接
    function _bannerItem(data) {
      return new Promise((resolve, reject) => {
        data.then(res => {
          res.data.forEach(data => {
            data.image = IMAGEPREFIX + data.image
          })
          resolve(res)
        })
      })
    }
    // 主题图片地址拼接
    function _themeItem(data) {
      return new Promise((resolve, reject) => {
        data.then(res => {
          console.log(res)
          res.data.forEach(data => {
            data.thumb = IMAGEPREFIX + data.thumb
          })
          resolve(res)
        })
      })
    }
    // 多个商品图片地址拼接
    function _productItem(data) {
      return new Promise((resolve, reject) => {
        data.then(res => {
          console.log(res);
          if(res.list&&res.list.length > 0){
            res.list[0].data.forEach(data => {
              data.thumb = IMAGEPREFIX + data.thumb
            });
            resolve(res);
          } else{
            res.list = [];
            reject(res)
          }
        })
      })
    }
    // 单个商品图片地址拼接菜
    function _productImg(data) {
      return new Promise((resolve, reject) => {
        data.then(res => {
          console.log(res)
          let data1 = res.list[0].thumb
          data1.forEach((data,index,array) => {
            array[index] = IMAGEPREFIX + array[index]
          });
          resolve(res);
        })
      })
    }
  
  
    return app.serve();
  }